package com.example.service.impl;

import com.example.entity.singerAndGame.GSRelative;
import com.example.entity.singerAndGame.Game;
import com.example.entity.singerAndGame.Singer;
import com.example.mapper.*;
import com.example.service.GameService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GameServiceImpl implements GameService {

    @Resource
    GameMapper mapper;

    @Resource
    GAndSMapper gAndSMapper;

    @Resource
    SingerMapper singerMapper;

    @Resource
    VoteMapper voteMapper;

    @Resource
    GameStatisticsMapper voteStatisticsMapper;

    @Override
    public List<Game> getGameList() {
        return mapper.getGameList();
    }

    @Override
    public List<Singer> getGameSingerListByGid(int gid) {
        List<GSRelative> gsRelativeList = gAndSMapper.getGSRelativeByGid(gid);
        List<Integer> sidList = new ArrayList<>();
        for (GSRelative gsRelative : gsRelativeList) {
            sidList.add(gsRelative.getSid());
        }
        List<Singer> singers = new ArrayList<>();
        for (Integer sid : sidList) {
            singers.add(singerMapper.getSingerBySid(sid));
        }
        return singers;
    }

    @Override
    public List<GSRelative> getGameSingerScoreListByGid(int gid) {
        return gAndSMapper.getGSRelativeByGid(gid);
    }

    @Override
    public Game getGameByGid(int gid) {
        return mapper.getGameByGid(gid);
    }

    @Override
    public String addGameOne(Game game) {
        return mapper.addGame(game) > 0 ? null : "新增比赛信息失败，请稍后再试";
    }

    @Override
    public String addGameTwo(int gid, List<Integer> sid) {
        if (gAndSMapper.getGSRelativeByGid(gid).isEmpty()) {
            for (Integer i : sid) {
                GSRelative gsRelative = new GSRelative();
                gsRelative.setGid(gid);
                gsRelative.setSid(i);
                gsRelative.setJudge_score(0.0);
                if (gAndSMapper.addGSRelative(gsRelative) < 0) return "参赛信息新增失败";
            }
        } else {
            gAndSMapper.deleteGSRelativeBySidOrGid(gid);
            for (Integer i : sid) {
                GSRelative gsRelative = new GSRelative();
                gsRelative.setGid(gid);
                gsRelative.setSid(i);
                gsRelative.setJudge_score(0.0);
                if (gAndSMapper.addGSRelative(gsRelative) < 0) return "参赛信息新增失败";
            }
        }
        return null;
    }

    @Override
    public String setJudgeScore(GSRelative gsRelative) {
        return gAndSMapper.updateScoreBySidAndGid(gsRelative) > 0 ? null : "评委评分失败";

    }

    @Override
    public String deleteGameByGid(int gid) {
        // 定义一个标志，用于表示是否成功删除了所有相关数据
        boolean successfullyDeleted = true;

        // 删除投票统计信息
        if (!voteStatisticsMapper.getGameStatisticsByGid(gid).isEmpty()) {
            successfullyDeleted = voteStatisticsMapper.deleteGameStatisticsByGid(gid) > 0;
        }

        // 删除投票记录
        if (successfullyDeleted && !voteMapper.getVoteRecordingByGid(gid).isEmpty()) {
            successfullyDeleted = voteMapper.deleteVoteRecordingByGid(gid) > 0;
        }

        // 删除比赛与其它实体的关联关系
        if (successfullyDeleted && !gAndSMapper.getGSRelativeByGid(gid).isEmpty()) {
            successfullyDeleted = gAndSMapper.deleteGSRelativeBySidOrGid(gid) > 0;
        }

        // 最后删除比赛本身
        if (successfullyDeleted) {
            successfullyDeleted = mapper.deleteGameByGid(gid) > 0;
        }

        // 根据删除结果返回相应的消息
        return successfullyDeleted ? null : "比赛信息删除失败";
    }

}
